<?php

class SF_AutoCoupon_Model_Subscriber extends Mage_Newsletter_Model_Subscriber
{
    const IS_COUPON_OF_SUBSCRIBER = "autocoupon/config/opencoupons";
    const PROMOTE_EVENTS = "autocoupon/config/promoteevents";
    const RULE_ITEM = "autocoupon/config/ruleitem";
    const COUPON_COUNT = "autocoupon/config/couponcount";
    /**
     * rewrite model of Mage_Newsletter_Model_Subscriber 
     * author :wang
     */
    
    public function isCouponOfSubscriber(){

        if(Mage::getStoreConfig(self::IS_COUPON_OF_SUBSCRIBER)){
            $events_code=Mage::getStoreConfig(self::PROMOTE_EVENTS);
            $sub_email = $this->getEmail();
            // $events_model var @SF_PromoteEvents_Helper_Data
            $events_model = Mage::helper('promoteevents')->getEvents($events_code);
            $even_info  = $events_model->getEventsData();
            $from_date = strtotime($even_info->getFromDate());
            $to_date     = strtotime($even_info->getToDate());
            $active       =$even_info->getIsActive();
            $now = Mage::getModel( 'core/date' )->timestamp(time());
            $couponLog = Mage::getModel("autocoupon/couponlog")->getCollection();
            $is_subscriber=$couponLog
            ->addFieldToFilter("events_code",array('eq'=>$events_code))
            ->addFieldToFilter("email",array('eq'=>$sub_email))
            ->getData();      
              if(!$is_subscriber&&$now>=$from_date&&$now<=$to_date&&$active==1) 
              {
                  return true;
              }
          }
      return false;
    }
    public function getCouponCode(){
        $rule_id=Mage::getStoreConfig(self::RULE_ITEM);
        $coupon_count =Mage::getStoreConfig(self::COUPON_COUNT);
        $rule_id = explode(",", $rule_id);
        $events_code=Mage::getStoreConfig(self::PROMOTE_EVENTS);
        $record=array(
            "email"=>$this->getEmail(),
            "events_code"=>$events_code,
        );
        if(count($rule_id)>1){
            foreach ($rule_id as $val ){
                $code[] = Mage::helper('autocoupon')->getCouponCode($val,$record);
            }
        }else{
                $code[] = Mage::helper('autocoupon')->getCouponCode($rule_id["0"],$record);
        } 
        $str.="<table style='border:solid #000000; border-width:1px 0px 0px 1px;border-collapse: collapse;text-align: center;width:520px'>";
        $str.="<tr><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;font-weight: bold;'>".Mage::helper('autocoupon')->__("Discount")."</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;font-weight: bold;'>".Mage::helper('autocoupon')->__("Coupon Code")."</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;font-weight: bold;'>".Mage::helper('autocoupon')->__("Order(s)")."</td></tr>";
       foreach($code as $key => $codes){
           $str.="<tr><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;'>".Mage::helper('autocoupon')->__("$".(int)$codes['amount']." OFF")."</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;'>";
           foreach ($codes['code'] as $cod ){
               $str.="<p style='font-weight: bold;color:#f2445d'>".$cod."</p>";
           }
           $str.="</td><td style='border:solid #000000; border-width:0px 1px 1px 0px; padding:5px 5px;'>".Mage::helper('autocoupon')->__($codes['description'])."</td></tr>";
       }
       $str.="</table>";
       return  $str;
    }
}